package com.nervenets.shopping.controller.user;

import com.nervenets.general.entity.ResponseResult;
import com.nervenets.general.utils.JodaUtils;
import com.nervenets.general.web.BaseController;
import com.nervenets.shopping.controller.user.dto.SystemStateVo;
import com.nervenets.shopping.controller.user.dto.UserStateVo;
import com.nervenets.shopping.controller.user.params.UserStateParams;
import com.nervenets.shopping.service.GroupBuyOrderService;
import com.nervenets.shopping.service.GroupBuyOrderUserService;
import com.nervenets.shopping.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.validation.Valid;

@Api(value = "App统计数据相关接口v1", tags = "App统计数据相关接口")
@Slf4j
@Controller
@RequestMapping("/api/app")
public class AppController extends BaseController {
    @Resource
    private UserService userService;
    @Resource
    private GroupBuyOrderService groupBuyOrderService;
    @Resource
    private GroupBuyOrderUserService groupBuyOrderUserService;

    @ApiOperation("统计拼友数据和参与人数")
    @PostMapping("/user/state")
    public ResponseEntity<ResponseResult<UserStateVo>> userState(@Valid @RequestBody UserStateParams params) {
        if (0 == params.getStartTime()) params.setStartTime(JodaUtils.getStartOfToday());
        if (0 == params.getEndTime()) params.setEndTime(JodaUtils.getEndOfToday());
        return successMessage(UserStateVo.builder()
                .memberCount(userService.count((predicates, root, query, builder) -> predicates.add(builder.isTrue(builder.function("JSON_CONTAINS", Boolean.class, root.get("parentIds"), builder.literal(String.valueOf(params.getTargetId())))))))
                .groupCount(groupBuyOrderUserService.countUserState(params.getTargetId(), params.getStartTime(), params.getEndTime()))
                .build());
    }

    @ApiOperation("统计系统总数据")
    @PostMapping("/system/state")
    public ResponseEntity<ResponseResult<SystemStateVo>> systemState() {
        return successMessage(SystemStateVo.builder()
                .groupSuccessCount(groupBuyOrderService.count((predicates, root, query, builder) -> predicates.add(builder.greaterThan(root.get("successTime"), 0))))
                .groupUserCount(userService.count())
                .sumOfContribution(groupBuyOrderService.sumOfContribution())
                .sumOfBonus(groupBuyOrderService.sumOfBonus())
                .sumOfReward(groupBuyOrderUserService.sumOfReward())
                .sumOfOrderMoney(groupBuyOrderUserService.sumOfOrderMoney())
                .sumOfRefundMoney(groupBuyOrderUserService.sumOfRefundMoney())
                .build());
    }
}
